import requests
import json
import datetime
from testresult import TestResult

'''测试结果上传'''
class UploadResult():

    def __init__(self, deviceid, result, modulename, productname, version, apkversion, tester, starttime, endtime, reportname):   
        
        self.result = result
        self.data = {}
        self.data['test_deviceid'] = deviceid
        self.data['test_projectname'] = productname

        if apkversion != None and apkversion != '':
            self.data['test_version'] = apkversion
        else:
            self.data['test_version'] = version
        
        self.data['tester'] = tester
        self.data['test_start_time'] = starttime.strftime('%Y-%m-%d %H:%M:%S')
        self.data['test_end_time'] = endtime.strftime('%Y-%m-%d %H:%M:%S')
        self.data['test_type'] = 'Monkey测试'
        self.data['test_module'] = modulename
        self.data['test_testcase'] = ''
        self.data['test_reportname'] = reportname
        
        timespan = (endtime - starttime)

        if timespan.seconds<90:
            self.data['test_use_minutes'] = '1'
        else:
            self.data['test_use_minutes'] = str(round(timespan.seconds/60))

        self.postdata_url = 'http://tdc.vivo.xyz/tdc/post_jenkins_json'
        self.upload_file_url = 'http://tdc.vivo.xyz/tdc/upload_file'
        
        pass
    
    def postdata(self):
        '''开始上传数据'''
        failnumber = 0 
        passnumber = 0

        for rt in self.result.result:
            rtlist = list(rt)
        
            if str(rtlist[0]).find('0')>=0:
                passnumber = passnumber + 1
            else:
                failnumber = failnumber + 1
            testcasename = str(rtlist[1]).replace('<','').replace('>','')

            if self.data['test_testcase'] == '':
                self.data['test_testcase'] = testcasename
            else:
                self.data['test_testcase'] = self.data['test_testcase']  + ',' + testcasename

        if failnumber>0:
            self.data['test_result'] = '失败'
        else:
            self.data['test_result'] = '成功'
        
        self.data['test_fail_number'] = str(failnumber)
        self.data['test_pass_number'] = str(passnumber)
        self.data['test_bug_number'] = str(failnumber)
        
        print('执行结果:'+self.data['test_result'])
        
        headers = {'Content-Type':'application/json'}
        value = json.dumps(self.data).encode('utf-8')
        #print(value)
        response = requests.post(self.postdata_url, data = value, headers = headers)
        if response.text == 'OK':
            print('==============数据上传成功===============')
        else:
            print('==============数据上传失败===============' + response.text)

    
    def upload_file(self, filepath):
        '''上传测试报告文件'''
        file = {'myfile': open(filepath,'rb')}
        response = requests.post(self.upload_file_url, files=file)
        if response.text == 'OK':
            print('==============数据文件成功================')
        else:
            print('==============数据文件失败================' + response.text)
